#define  _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <climits>
using namespace std;

class Solution {
public:
    vector<int> findClosedNumbers(int num) {
        if (num == INT_MAX) return { -1 ,-1 };
        int bigger = 0;
        int curnumof1 = 0;
        int copy_num = num;
        for (int i = 0; i < 31; i++)
        {
            /*if (num & (1 << i) && !(num & (1 << (i + 1))))
            {
                bigger = (num | (1 << (i + 1))) & ~(1 << i);
                break;
            }*/
            if (num & (1 << i))
            {
                curnumof1++;
                copy_num = copy_num & ~(1 << i);
                if (!(num & (1 << (i + 1))))
                {
                    copy_num |= 1 << (i + 1);
                    for (int j = 0; j < curnumof1 - 1; j++)
                    {
                        copy_num |= 1 << j;
                    }
                    bigger = copy_num;
                    break;
                }
            }
        }
        int less = 0;
        curnumof1 = 0;
        copy_num = num;
        for (int i = 0; i < 32; i++)
        {
            if (num & (1 << i))
            {
                curnumof1++;
                copy_num = copy_num & ~(1 << i);
                if (i && !(num & (1 << (i - 1))))
                {
                    for (int j = 1; j <= curnumof1; j++)
                    {
                        copy_num |= 1 << (i - j);
                    }
                    less = copy_num;
                    break;
                }
            }
        }
        if (!less)
            less = -num;
        return { bigger, less };
    }
};

int main()
{
    Solution().findClosedNumbers(124);
}